package com.itheima.health.controller;


import com.itheima.health.common.PageParam;
import com.itheima.health.common.R;
import com.itheima.health.common.ResultPageData;

import com.itheima.health.model.dtos.AddSetMealDto;
import com.itheima.health.model.dtos.UpdateSetMealDto;
import com.itheima.health.model.pojos.Setmeal;
import com.itheima.health.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    ///findPage
    @Autowired
    private SetMealService setMealService;

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @PostMapping("/findPage")
    public ResultPageData getSetMealPage(@RequestBody PageParam pageParam){
        return setMealService.getSetMealPage(pageParam);
    }

    /**
     * 图片上传
     * @return
     */
    @PostMapping("/upload")
    public R uploadImageById(@RequestParam("imgFile") MultipartFile file) throws Exception {
       String url= setMealService.uploadImageById(file);
        R r = new R();
        r.setFlag(true);
        r.setData(url);
        r.setMessage("上传成功");
        return r;
    }

    /**
     * 添加套餐
     * @param ids
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public R insertCheckGroupsWithSetMealId(@RequestParam("checkgroupIds") List<Integer> ids,@RequestBody AddSetMealDto dto){
        return   setMealService.insertCheckGroupsWithSetMealId(ids,dto);
    }

    /**
     * 修改回显
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public R getSetMealById(Integer id){
       Setmeal setmeal= setMealService.getSetMealById(id);
        R r = new R();
        r.setFlag(true);
        r.setData(setmeal);
        return r;
    }

    /**
     * 修改
     * @param dto
     * @return
     */
    @PostMapping("/edit")
    public R updateSetMealById(@RequestParam("checkgroupIds") List<Integer> checkgroupIds,@RequestBody UpdateSetMealDto dto){
        return   setMealService.updateSetMealById(checkgroupIds,dto);

    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/deletesetmeal")
    public R deleteSetMealById(Integer id){
        return setMealService.deleteSetMealById(id);
    }

}
